using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using objetos;
using dados;
using System.IO;
using System.Xml.Serialization;

namespace negocios
{
	#region TB_NF_ITEM_IMPOSTO_II
	/// <summary>
	/// This object represents the properties and methods of a TB_NF_ITEM_IMPOSTO_II.
	/// </summary>
	public class BLL_TB_NF_ITEM_IMPOSTO_II : System.Web.UI.Page
	{		
		DAL_TB_NF_ITEM_IMPOSTO_II DAL = new DAL_TB_NF_ITEM_IMPOSTO_II();
		
		#region METODOS PROCEDURES

        public DataTable Dt_nf_item_imposto_ii
        {
            get
            {
                if (Session["dt_nt_item_imposto_ii"] == null)
                {
                    Dt_nf_item_imposto_ii = CriarDataTable();
                }
                return (DataTable)Session["dt_nt_item_imposto_ii"];
            }
            set { Session["dt_nt_item_imposto_ii"] = value; }
        }

		/// <summary>
		/// Incluir via procedure
		/// </summary>
		public void Incluir(OBJ_TB_NF_ITEM_IMPOSTO_II obj)
		{
			DAL.Incluir(obj);
			
		}
		
		/// <summary>
		/// Alterar via procedure
		/// </summary>
		public void Alterar(OBJ_TB_NF_ITEM_IMPOSTO_II obj)
		{
			DAL.Alterar(obj);	
		}
		
		/// <summary>
		/// Excluir via procedure
		/// </summary>
		public void Excluir(OBJ_TB_NF_ITEM_IMPOSTO_II obj)
		{
			DAL.Excluir(obj);	
		}
		
		/// <summary>
		/// Excluir Lista via procedure
		/// </summary>
		public void ExcluirLista(List<OBJ_TB_NF_ITEM_IMPOSTO_II> lista)
		{
				foreach (OBJ_TB_NF_ITEM_IMPOSTO_II obj in lista)
            	{
                this.Excluir(obj);
            	}
			
		}
						
		/// <summary>
		/// Pesquisar Parametros Where, OrderBy, PageIndex, PageSize
		/// </summary>		
		public DataSet Selecionar(string Where, string OrderBy, int PageIndex, int PageSize)
		{
			
			return DAL.Selecionar(Where,OrderBy,PageIndex,PageSize);
			
		}		
		
		
		/// <summary>
		/// Retorna Objeto por ID
		/// </summary>
		public OBJ_TB_NF_ITEM_IMPOSTO_II SelecionarObjetoPorId(int id)
		{
			return DAL.SelecionarObjetoPorId(id);
			
		}
				
			
		/// <summary>
		/// Retorna DataSet Parametros Where, OrderBy
		/// </summary>	
		public DataSet Selecionar(String Where, String OrderBy)
        {
            return DAL.Selecionar(Where, OrderBy);
        }
		
		/// <summary>
		/// Retorna DataSet
		/// </summary>	
		public DataSet Selecionar()
        {
            return DAL.Selecionar();
        }
			
		
		/// <summary>
		/// Retorna uma lista de objetos por parametro Where, OrderBy
		/// </summary>
		public List<OBJ_TB_NF_ITEM_IMPOSTO_II>SelecionarLista(String Where, String OrderBy)
		{						
			return 	DAL.SelecionarLista(Where,OrderBy);
		}
		
		
		/// <summary>
		/// Retorna uma lista de objetos por parametro 
		/// </summary>
		public List<OBJ_TB_NF_ITEM_IMPOSTO_II>SelecionarLista()
		{						
			return 	DAL.SelecionarLista();
		}
		
		public void PreencheControle(System.Web.UI.Control controle, bool selecione)
        {
            if (controle is DropDownList)
            {
                DropDownList ddl = (DropDownList)controle;
                ddl.Items.Clear();
                foreach (OBJ_TB_NF_ITEM_IMPOSTO_II obj in DAL.SelecionarLista())
                {
                    ddl.Items.Add(new ListItem(obj.valor_bc_ii.ToString(), obj.id_nf_item.ToString()));
                }
                
                if (selecione)
                {
                    ddl.Items.Insert(0, new ListItem("Selecione", ""));
                    ddl.SelectedIndex = 0;
                }

                ddl.DataBind();
            }


            if (controle is ListBox)
            {
                ListBox lst = (ListBox)controle;
                lst.Items.Clear();
                foreach (OBJ_TB_NF_ITEM_IMPOSTO_II obj in DAL.SelecionarLista())
                {
                    lst.Items.Add(new ListItem(obj.valor_bc_ii.ToString(), obj.id_nf_item.ToString()));
                }

                if (selecione)
                {
                    lst.Items.Insert(0, new ListItem("Selecione", ""));
                    lst.SelectedIndex = 0;
                }

                lst.DataBind();
            }

        }										
			
		
		public DataSet ListaCampos()
		{
			 return DAL.ListaCampos();
		}

        public DataTable CriarDataTable()
        {
            return DAL.CriarDataTable();
        }


        public DataTable PreencheDataTable(DataTable dt, OBJ_TB_NF_ITEM_IMPOSTO_II item)
        {

            return DAL.PreencheDataTable(dt, item);
        }


        public void SerializeLista(List<OBJ_TB_NF_ITEM_IMPOSTO_II> obj, string arquivo)
        {

            try
            {
                // C#
                // Create file to save the data to
                FileStream fs = new FileStream(arquivo, FileMode.Create);
                // Create an XmlSerializer object to perform the serialization
                XmlSerializer xs = new XmlSerializer(typeof(List<OBJ_TB_NF_ITEM_IMPOSTO_II>));
                // Use the XmlSerializer object to serialize the data to the file
                xs.Serialize(fs, obj);
                // Close the file
                fs.Close();
            }
            catch (Exception)
            {

                throw;
            }



        }


        public List<OBJ_TB_NF_ITEM_IMPOSTO_II> DeserializeLista(string arquivo)
        {
            try
            {
                // C#
                // Open file to read the data from
                FileStream fs = new FileStream(arquivo, FileMode.Open);
                // Create an XmlSerializer object to perform the deserialization
                XmlSerializer xs = new XmlSerializer(typeof(List<OBJ_TB_NF_ITEM_IMPOSTO_II>));
                // Use the XmlSerializer object to deserialize the data from the file
                List<OBJ_TB_NF_ITEM_IMPOSTO_II> obj = (List<OBJ_TB_NF_ITEM_IMPOSTO_II>)xs.Deserialize(fs);
                // Close the file
                fs.Close();

                return obj;

            }
            catch (Exception)
            {

                throw;
            }

        }

        public void SerializeObjeto(OBJ_TB_NF_ITEM_IMPOSTO_II obj, string arquivo)
        {

            try
            {
                // C#
                // Create file to save the data to
                FileStream fs = new FileStream(arquivo, FileMode.Create);
                // Create an XmlSerializer object to perform the serialization
                XmlSerializer xs = new XmlSerializer(typeof(OBJ_TB_NF_ITEM_IMPOSTO_II));
                // Use the XmlSerializer object to serialize the data to the file
                xs.Serialize(fs, obj);
                // Close the file
                fs.Close();
            }
            catch (Exception)
            {

                throw;
            }



        }


        public OBJ_TB_NF_ITEM_IMPOSTO_II DeserializeObjeto(string arquivo)
        {
            try
            {
                // C#
                // Open file to read the data from
                FileStream fs = new FileStream(arquivo, FileMode.Open);
                // Create an XmlSerializer object to perform the deserialization
                XmlSerializer xs = new XmlSerializer(typeof(OBJ_TB_NF_ITEM_IMPOSTO_II));
                // Use the XmlSerializer object to deserialize the data from the file
                OBJ_TB_NF_ITEM_IMPOSTO_II obj = (OBJ_TB_NF_ITEM_IMPOSTO_II)xs.Deserialize(fs);
                // Close the file
                fs.Close();

                return obj;

            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// Soma Valor
        /// </summary>
        /// <param name="tabela"></param>
        /// <returns></returns>
        public Decimal SomaValor(DataTable tabela, string coluna, string param)
        {
            DataTable dt = tabela;

            decimal valor_total = 0;

            try
            {
                if (dt != null)
                {
                    object resultado = dt.Compute(string.Format("SUM({0})", coluna), param);
                    if (resultado != DBNull.Value)
                    {
                        valor_total = Convert.ToDecimal(resultado);
                        return valor_total;
                    }
                    else
                    {
                        return valor_total;
                    }

                }
                else
                {
                    return valor_total;
                }

            }
            catch (Exception)
            {
                throw;
            }



        }

        public Decimal SomaValor(DataTable tabela, string coluna)
        {
            DataTable dt = tabela;

            decimal valor = 0;

            try
            {
                if (dt != null)
                {
                    object resultado = dt.Compute(string.Format("SUM({0})", coluna), "");

                    if (resultado != DBNull.Value)
                    {
                        valor = Convert.ToDecimal(resultado);
                        return valor;

                    }
                    else
                    {
                        return valor;
                    }


                }
                else
                {
                    return valor;
                }

            }
            catch (Exception)
            {
                throw;
            }



        }
					

		#endregion
	}
	#endregion
}

